package com.inspinia.config.common.config;

import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;

import java.util.ResourceBundle;

/**
 * db配置
 * 优先使用配置中心的配置，如果没有配置文件使用本地配置
 * @author Veblen
 */
@Slf4j
@Data
public class DbConfig {

    /**
     * db.properties的配置数据
     */
    public static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle("db");
    public static String getString(String key) {
        return RESOURCE_BUNDLE.getString(key);
    }

    public static String driver = getString("master.jdbc.driver");
    public static String url = getString("master.jdbc.url");
    public static String username = getString("master.jdbc.username");
    public static String password = getString("master.jdbc.password");

    public static int druidMaxActive = Integer.valueOf(getString("druid.maxActive"));
    public static int druidMinPoolsize = Integer.valueOf(getString("druid.minPoolSize"));
    public static int druidMaxIdle = Integer.valueOf(getString("druid.maxIdle"));
    public static int druidRemoveabAndOnedTimeout = Integer.valueOf(getString("druid.removeAbandonedTimeout"));

    /**
     *  监听配置
     * @param changeEvent
     */
    @ApolloConfigChangeListener("db")
    public void dbChange(ConfigChangeEvent changeEvent){
        log.info("Changes for namespace {}", changeEvent.getNamespace());
        for (String key : changeEvent.changedKeys()) {
            ConfigChange change = changeEvent.getChange(key);
            log.info("Change - key: {}, oldValue: {}, newValue: {}, changeType: {}",
                    change.getPropertyName(), change.getOldValue(), change.getNewValue(),
                    change.getChangeType());
            if ("master.jdbc.driver".equals(change.getPropertyName())) {
                driver = change.getNewValue();
            }
            if ("master.jdbc.url".equals(change.getPropertyName())) {
                url = change.getNewValue();
            }
            if ("master.jdbc.username".equals(change.getPropertyName())) {
                username = change.getNewValue();
            }
            if ("master.jdbc.password".equals(change.getPropertyName())) {
                password = change.getNewValue();
            }
            if ("druid.maxActive".equals(change.getPropertyName())) {
                druidMaxActive = Integer.valueOf(change.getNewValue());
            }
            if ("druid.minPoolSize".equals(change.getPropertyName())) {
                druidMinPoolsize = Integer.valueOf(change.getNewValue());
            }
            if ("druid.maxIdle".equals(change.getPropertyName())) {
                druidMaxIdle = Integer.valueOf(change.getNewValue());
            }
            if ("druid.removeAbandonedTimeout".equals(change.getPropertyName())) {
                druidRemoveabAndOnedTimeout = Integer.valueOf(change.getNewValue());
            }
        }
    }
}
